package leetcode.code1781;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int beautySum(String s) {
		int ans = 0;
		int len = s.length();
		char[] cs = s.toCharArray();
		int[][] freqs = new int[len + 1][26];
		int[] freq = new int[26];
		for (int i = 1; i <= len; i++) {
			int cur = cs[i - 1];
			freq[cur - 'a']++;
			freqs[i] = Arrays.copyOf(freq, 26);
		}
		for (int i = 3; i <= len; i++) {
			for (int j = 0; j < i - 2; j++) {
				int min = Integer.MAX_VALUE;
				int max = Integer.MIN_VALUE;
				int[] freqi = freqs[i];
				int[] freqj = freqs[j];
				for (int k = 0; k < 26; k++) {
					int sub = freqi[k] - freqj[k];
					if (sub != 0) {
						if (sub > max) {
							max = sub;
						}
						if (sub < min) {
							min = sub;
						}
					}
				}
//				if (min != Integer.MAX_VALUE)
				ans += max - min;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(5, this.beautySum("aabcb"));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
